home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / OSAX Samples / Basic OSAX / FontAscent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-07  |  2.5 KB  |  104 lines  |  [TEXT/KAHL]

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    FontAscent.c
  4. //    Written by Donald Olson
  5. //    Copyright ®1993 Apple Computer Inc.
  6. //    All rights reserved.
  7. //
  8. //    This is a simple Scripting Addition that returns the font ascent of the
  9. //    specified font and size. If the size parameter is not sp[ecified, default
  10. //    size of 12 is used. Written for a WWDC presentation by Donald Olson
  11. //    and Donn Denman.
  12. //
  13. //    Syntax: font ascent <font name [size <short>]
  14. //    Returns: short
  15. //
  16. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  17.  
  18. // Our includes
  19. #include <string.h>
  20. #include <Quickdraw.h>
  21. #include <Fonts.h>
  22. #include <Errors.h>
  23. #include <AppleEvents.h>
  24.  
  25. // Our header
  26. Boolean GetFontNumber( Str255 fontName, short *fontNum );
  27.  
  28.  
  29. /*
  30. This is the structure returned from FontInfo.
  31. struct FontInfo {
  32.     short ascent;
  33.     short descent;
  34.     short widMax;
  35.     short leading;
  36. };
  37. */
  38. //AEVTFINFsent
  39. #define keyFONTSIZE    'FSIZ'
  40.  
  41. pascal OSErr main(AppleEvent *theEvent, 
  42.                 AppleEvent *theReply, 
  43.                 long theRefCon) 
  44. {            
  45.     OSErr        theErr = noErr;
  46.     GrafPort        fontPort;
  47.     GrafPtr        savedPort, fontPortPtr = &fontPort;
  48.     Str255        fontName;
  49.     long            actualSize;
  50.     short        fontSize, fontNum;
  51.     DescType    typeCode;
  52.     FontInfo        fInfo;
  53.  
  54.     theErr = AEGetParamPtr(theEvent, keyDirectObject, 
  55.                 typeChar, &typeCode, (Ptr)&fontName, 
  56.                 sizeof(fontName), &actualSize);
  57.     
  58.     if(theErr != noErr)
  59.         return theErr;
  60.     
  61.     fontName[actualSize] = '\0';        // c string please
  62.     c2pstr((char*)fontName);        // and now pascal
  63.     
  64.     theErr = AEGetParamPtr(theEvent, keyFONTSIZE, 
  65.                 typeShortInteger, &typeCode, (Ptr)&fontSize, sizeof(fontSize), &actualSize);
  66.     
  67.     if(theErr != noErr)
  68.         fontSize = 12;                // default size will be 12
  69.     
  70.     if(!GetFontNumber(fontName, &fontNum))
  71.         return fontDecError;
  72.                 
  73.     GetPort(&savedPort);
  74.     OpenPort(&fontPortPtr);
  75.     SetPort(&fontPortPtr);
  76.     
  77.     TextFont(fontNum);
  78.     TextSize(fontSize);
  79.     
  80.     GetFontInfo(&fInfo);
  81.     
  82.     SetPort(&savedPort);
  83.     
  84.     theErr = AEPutParamPtr(    theReply, keyDirectObject, typeShortInteger, 
  85.                             (Ptr)&fInfo.ascent, sizeof(fInfo.ascent));
  86.     
  87.     return theErr;
  88. }
  89.  
  90. Boolean GetFontNumber( Str255 fontName, short *fontNum )
  91. {
  92.     Str255     systemFontName;
  93.  
  94.     GetFNum( fontName, fontNum );
  95.     if ( *fontNum == 0) {
  96.         /* Either we didn't find the font, or we were looking for the system
  97.           * font. */
  98.         GetFontName( 0, systemFontName );
  99.         return EqualString( fontName, systemFontName, FALSE, FALSE );
  100.     }
  101.     else
  102.         return TRUE;
  103. }
  104.